package com.lkd.http.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.wxpay.sdk.WXPayUtil;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.entity.OrderEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.OrderService;
import com.lkd.utils.ConvertUtils;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.OrderVO;
import com.lkd.vo.Pager;
import com.lkd.vo.PayVO;
import com.lkd.wxpay.WXConfig;
import com.lkd.wxpay.WxPayDTO;
import com.lkd.wxpay.WxPaySDKUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPaySDKUtil wxPaySDKUtil;

    @Autowired
    private RedissonClient redissonClient;


    /**
     * 订单查询
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @param openId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/search")
    public Pager<OrderVO> search(
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(value = "orderNo",required = false,defaultValue = "") String orderNo,
            @RequestParam(value = "openId",required = false,defaultValue = "") String openId,
            @RequestParam(value = "startDate",required = false,defaultValue = "") String startDate,
            @RequestParam(value = "endDate",required = false,defaultValue = "") String endDate){
        return orderService.search(pageIndex,pageSize,orderNo,openId,startDate,endDate);
    }


    /**
     * 获取商圈下3个月内销量前10商品
     * @param businessId
     * @return
     */
    @GetMapping("/businessTop10/{businessId}")
    public List<Long> getBusinessTop10Skus(@PathVariable Integer businessId){
        return orderService.getTop10Sku(businessId);
    }


    /**
     * 微信下单
     */
    @PostMapping("/weixinPay")
    public String weixinPay(@RequestBody PayVO payVO) throws InterruptedException {
        RLock lock = redissonClient.getLock(String.format(VMSystem.ORDER_LOCK_KEY, payVO.getInnerCode()));
        if (!lock.tryLock(0L,1L, TimeUnit.SECONDS)){
            throw new LogicException("商品正在出货中,请稍后");
        }
        return orderService.weixinPay(payVO);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            System.out.println(String.format("%d0",i));
        }
    }

    /**
     * 微信支付回调接口
     * @param request
     * @return
     */
    @RequestMapping("/payNotify")
    @ResponseBody
    public void payNotify(HttpServletRequest request, HttpServletResponse response){
        //将返回的流转换为xml格式的字符串
        ServletInputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            String convert = ConvertUtils.convertToString(inputStream);
            String orderNo = wxPaySDKUtil.validPay(convert);
            //通知出货
            orderService.vendout(orderNo);
            response.setContentType("text/xml");
            response.getWriter().write(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
